(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 100;
    vc.extends({
        data: {
            userAddresses:[],
            provs: [],
            citys: [],
            areas: [],
            addAddressInfo: {
                selectProv: '',
                selectCity: '',
                selectArea: '',
                addressId: '',
                userId: '',
                area: '',
                address: '',
                areaCode: '',
                province: '',
                city: '',
                postalCode: '',
                district: '',
                telNum: '',
                areaName:'',
                extensionNum: '',
                tel: '',
                zipCode: '',
                isDefault: '',
                createUser: '',
                createOrg: '',
                updateUser: '',
                updateOrg: '',
                provinceStr: '',
                cityStr: '',
                districtStr: '',
            },
        },
        _initMethod: function () {
            vc.component._listAddresses(DEFAULT_PAGE, DEFAULT_ROWS);
        },
        _initEvent: function () {
            vc.component._selectArea('101', '0');
        },
        methods: {
            getProv: function (_prov) {
                vc.component._selectArea('202', _prov);
            },
            getCity: function (_city) {
                vc.component._selectArea('303', _city);
            },
            getArea: function (_area) {
                vc.component.addAddressInfo.areaName = '';
                vc.component.addAddressInfo.areaCode = _area;
                if (!vc.component.provs) {
                    return;
                }
                vc.component.provs.forEach(function (_param) {
                    if (_param.areaCode == vc.component.addAddressInfo.selectProv) {
                        vc.component.addAddressInfo.areaName = _param.areaName;
                    }
                });
                vc.component.citys.forEach(function (_param) {
                    if (_param.areaCode == vc.component.addAddressInfo.selectCity) {
                        vc.component.addAddressInfo.areaName += _param.areaName;
                    }
                });
                vc.component.areas.forEach(function (_param) {
                    if (_param.areaCode == vc.component.addAddressInfo.selectArea) {
                        vc.component.addAddressInfo.areaName += _param.areaName;
                    }
                });
            },
            _selectArea: function (_areaLevel, _parentAreaCode) {
                let _param = {
                    params: {
                        areaLevel: _areaLevel,
                        parentAreaCode: _parentAreaCode
                    }
                };
                vc.http.apiGet('/area.listAreas',
                    _param,
                    function (json, res) {
                        if (res.status == 200) {
                            let _tmpAreas = JSON.parse(json).data;
                            if (_areaLevel == '101') {
                                vc.component.provs = _tmpAreas;
                            } else if (_areaLevel == '202') {
                                vc.component.citys = _tmpAreas;
                            } else {
                                vc.component.areas = _tmpAreas;
                            }
                            return;
                        }
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理', errInfo, error);
                        vc.toast("查询地区失败");
                    });
            },
            _listAddresses: function (_page, _rows) {
                var param = {
                    params: {page:_page,row:_rows,userId:vc.getData('/nav/getUserInfo').userId}
                };
                //发送get请求
                vc.http.apiGet('userAddress.queryUserAddress',
                    param,
                    function (json, res) {
                        var _addressInfo = JSON.parse(json);
                        vc.component.userAddresses = _addressInfo.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            addAddr: function () {
                $("#mask").css("height", $(document).height()).show();
                $(".edit-address").show();
                $("#id").val('');
                $("#sh-name").val('');
                $("#sh-phone").val($("#user_mobile").val());
                $("#sh-tel").val('');
                $("#district").val('');
                $("#sh-address").val('');
                $("#sh-postcode").val('');
                $("#isdefault").removeAttr("checked");
                vc.component._selectArea('101', '0');
            },
            closeTips:function(){
                $("#mask").hide();
                $(".edit-address").hide();
            },
            saveAddressInfo: function () {
                vc.component.addAddressInfo.userId = vc.getData('/nav/getUserInfo').userId;
                if (!vc.component.addAddressValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                if (!vc.component.addAddressInfo.isDefault) {
                    vc.component.addAddressInfo.isDefault = '0';
                } else {
                    vc.component.addAddressInfo.isDefault = '1';
                }
                vc.http.apiPost(
                    'userAddress.saveUserAddress',
                    JSON.stringify(vc.component.addAddressInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            vc.component._listAddresses(DEFAULT_PAGE, DEFAULT_ROWS);
                            $("#mask").hide();
                            $(".edit-address").hide();
                            return;
                        }
                        vc.toast(_json.msg);

                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');

                        vc.toast(errInfo);

                    });
            },
            addAddressValidate() {
                return vc.validate.validate({
                    addAddressInfo: vc.component.addAddressInfo
                }, {
                    'addAddressInfo.username': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "收货人名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "30",
                            errInfo: "收货人名称不能超过30"
                        },
                    ],
                    'addAddressInfo.areaName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "收货区域不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "50",
                            errInfo: "收货区域不能超过50"
                        },
                    ],
                    'addAddressInfo.address': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "详细地址不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "50",
                            errInfo: "详细地址不能超过50"
                        },
                    ],
                    'addAddressInfo.tel': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "手机号码不能为空"
                        },
                        {
                            limit: "phone",
                            param: "",
                            errInfo: "手机号格式错误"
                        },
                    ],
                });
            },
            deleteAddress:function(_addr){
                vc.http.apiPost(
                    'userAddress.deleteUserAddress',
                    JSON.stringify(_addr),
                    {
                        emulateJSON:true
                    },
                    function(json,res){
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            vc.component._listAddresses(DEFAULT_PAGE, DEFAULT_ROWS);
                            return ;
                        }
                        vc.message(_json.msg);
                    },
                    function(errInfo,error){
                        console.log('请求失败处理');
                        vc.message(json);
                    });
            },
        }
    });

})(window.vc);